home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / unix / unz50p1.zoo / explode.c < prev    next >
C/C++ Source or Header  |  1992-06-27  |  27KB  |  794 lines

  1. /* explode.c -- Not copyrighted 1992 by Mark Adler
  2.    version c7, 27 June 1992 */
  3.  
  4.  
  5. /* You can do whatever you like with this source file, though I would
  6.    prefer that if you modify it and redistribute it that you include
  7.    comments to that effect with your name and the date.  Thank you.
  8.  
  9.    History:
  10.    vers    date          who           what
  11.    ----  ---------  --------------  ------------------------------------
  12.     c1   30 Mar 92  M. Adler        explode that uses huft_build from inflate
  13.                                     (this gives over a 70% speed improvement
  14.                                     over the original unimplode.c, which
  15.                                     decoded a bit at a time)
  16.     c2    4 Apr 92  M. Adler        fixed bug for file sizes a multiple of 32k.
  17.     c3   10 Apr 92  M. Adler        added a little memory tracking if DEBUG
  18.     c4   11 Apr 92  M. Adler        added NOMEMCPY do kill use of memcpy()
  19.     c5   21 Apr 92  M. Adler        added the WSIZE #define to allow reducing
  20.                                     the 32K window size for specialized
  21.                                     applications.
  22.     c6   31 May 92  M. Adler        added typecasts to eliminate some warnings
  23.     c7   27 Jun 92  G. Roelofs      added more typecasts
  24.  */
  25.  
  26.  
  27. /*
  28.    Explode imploded (PKZIP method 6 compressed) data.  This compression
  29.    method searches for as much of the current string of bytes (up to a length
  30.    of ~320) in the previous 4K or 8K bytes.  If it doesn't find any matches
  31.    (of at least length 2 or 3), it codes the next byte.  Otherwise, it codes
  32.    the length of the matched string and its distance backwards from the
  33.    current position.  Single bytes ("literals") are preceded by a one (a
  34.    single bit) and are either uncoded (the eight bits go directly into the
  35.    compressed stream for a total of nine bits) or Huffman coded with a
  36.    supplied literal code tree.  If literals are coded, then the minimum match
  37.    length is three, otherwise it is two.
  38.    
  39.    There are therefore four kinds of imploded streams: 8K search with coded
  40.    literals (min match = 3), 4K search with coded literals (min match = 3),
  41.    8K with uncoded literals (min match = 2), and 4K with uncoded literals
  42.    (min match = 2).  The kind of stream is identified in two bits of a
  43.    general purpose bit flag that is outside of the compressed stream.
  44.    
  45.    Distance-length pairs are always coded.  Distance-length pairs for matched
  46.    strings are preceded by a zero bit (to distinguish them from literals) and
  47.    are always coded.  The distance comes first and is either the low six (4K)
  48.    or low seven (8K) bits of the distance (uncoded), followed by the high six
  49.    bits of the distance coded.  Then the length is six bits coded (0..63 +
  50.    min match length), and if the maximum such length is coded, then it's
  51.    followed by another eight bits (uncoded) to be added to the coded length.
  52.    This gives a match length range of 2..320 or 3..321 bytes.
  53.  
  54.    The literal, length, and distance codes are all represented in a slightly
  55.    compressed form themselves.  What is sent are the lengths of the codes for
  56.    each value, which is sufficient to construct the codes.  Each byte of the
  57.    code representation is the code length (the low four bits representing
  58.    1..16), and the number of values sequentially with that length (the high
  59.    four bits also representing 1..16).  There are 256 literal code values (if
  60.    literals are coded), 64 length code values, and 64 distance code values,
  61.    in that order at the beginning of the compressed stream.  Each set of code
  62.    values is preceded (redundantly) with a byte indicating how many bytes are
  63.    in the code description that follows, in the range 1..256.
  64.  
  65.    The codes themselves are decoded using tables made by huft_build() from
  66.    the bit lengths.  That routine and its comments are in the inflate.c
  67.    module.
  68.  */
  69.  
  70. #include "unzip.h"      /* this must supply the slide[] (byte) array */
  71.  
  72. #ifndef WSIZE
  73. #  define WSIZE 0x8000  /* window size--must be a power of two, and at least
  74.                            8K for zip's implode method */
  75. #endif /* !WSIZE */
  76.  
  77.  
  78. struct huft {
  79.   byte e;               /* number of extra bits or operation */
  80.   byte b;               /* number of bits in this code or subcode */
  81.   union {
  82.     UWORD n;            /* literal, length base, or distance base */
  83.     struct huft *t;     /* pointer to next level of table */
  84.   } v;
  85. };
  86.  
  87. /* Function prototypes */
  88. /* routines from inflate.c */
  89. extern unsigned hufts;
  90. int huft_build OF((unsigned *, unsigned, unsigned, UWORD *, UWORD *,
  91.                    struct huft **, int *));
  92. int huft_free OF((struct huft *));
  93. void flush OF((unsigned));
  94.  
  95. /* routines here */
  96. int get_tree OF((unsigned *, unsigned));
  97. int explode_lit8 OF((struct huft *, struct huft *, struct huft *,
  98.                      int, int, int));
  99. int explode_lit4 OF((struct huft *, struct huft *, struct huft *,
  100.                      int, int, int));
  101. int explode_nolit8 OF((struct huft *, struct huft *, int, int));
  102. int explode_nolit4 OF((struct huft *, struct huft *, int, int));
  103. int explode OF((void));
  104.  
  105.  
  106. /* The implode algorithm uses a sliding 4K or 8K byte window on the
  107.    uncompressed stream to find repeated byte strings.  This is implemented
  108.    here as a circular buffer.  The index is updated simply by incrementing
  109.    and then and'ing with 0x0fff (4K-1) or 0x1fff (8K-1).  Here, the 32K
  110.    buffer of inflate is used, and it works just as well to always have
  111.    a 32K circular buffer, so the index is anded with 0x7fff.  This is
  112.    done to allow the window to also be used as the output buffer. */
  113. /* This must be supplied in an external module useable like "byte slide[8192];"
  114.    or "byte *slide;", where the latter would be malloc'ed.  In unzip, slide[]
  115.    is actually a 32K area for use by inflate, which uses a 32K sliding window.
  116.  */
  117.  
  118.  
  119. /* Tables for length and distance */
  120. UWORD cplen2[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  121.         18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
  122.         35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
  123.         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65};
  124. UWORD cplen3[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  125.         19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
  126.         36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  127.         53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66};
  128. UWORD extra[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  129.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  130.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  131.         8};
  132. UWORD cpdist4[] = {1, 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705,
  133.         769, 833, 897, 961, 1025, 1089, 1153, 1217, 1281, 1345, 1409, 1473,
  134.         1537, 1601, 1665, 1729, 1793, 1857, 1921, 1985, 2049, 2113, 2177,
  135.         2241, 2305, 2369, 2433, 2497, 2561, 2625, 2689, 2753, 2817, 2881,
  136.         2945, 3009, 3073, 3137, 3201, 3265, 3329, 3393, 3457, 3521, 3585,
  137.         3649, 3713, 3777, 3841, 3905, 3969, 4033};
  138. UWORD cpdist8[] = {1, 129, 257, 385, 513, 641, 769, 897, 1025, 1153, 1281,
  139.         1409, 1537, 1665, 1793, 1921, 2049, 2177, 2305, 2433, 2561, 2689,
  140.         2817, 2945, 3073, 3201, 3329, 3457, 3585, 3713, 3841, 3969, 4097,
  141.         4225, 4353, 4481, 4609, 4737, 4865, 4993, 5121, 5249, 5377, 5505,
  142.         5633, 5761, 5889, 6017, 6145, 6273, 6401, 6529, 6657, 6785, 6913,
  143.         7041, 7169, 7297, 7425, 7553, 7681, 7809, 7937, 8065};
  144.  
  145.  
  146. /* Macros for inflate() bit peeking and grabbing.
  147.    The usage is:
  148.    
  149.         NEEDBITS(j)
  150.         x = b & mask_bits[j];
  151.         DUMPBITS(j)
  152.  
  153.    where NEEDBITS makes sure that b has at least j bits in it, and
  154.    DUMPBITS removes the bits from b.  The macros use the variable k
  155.    for the number of bits in b.  Normally, b and k are register
  156.    variables for speed.
  157.  */
  158.  
  159. extern UWORD bytebuf;           /* (use the one in inflate.c) */
  160. #define NEXTBYTE    (ReadByte(&bytebuf), bytebuf)
  161. #define NEEDBITS(n) {while(k<(n)){b|=((ULONG)NEXTBYTE)<<k;k+=8;}}
  162. #define DUMPBITS(n) {b>>=(n);k-=(n);}
  163.  
  164.  
  165.  
  166. int get_tree(l, n)
  167. unsigned